<template>
    <section class="form-container">
        <el-form :inline="true" :model="filter" size="mini" class="form-filter">
            <el-row>
                <el-col :span="20">
                    <el-form-item label="广告分类" prop="adTypeId" size="mini">
                        <el-cascader
                            v-model="filter.cascaderPath"
                            :options="adTypeList"
                            :props="{ value: 'id',label:'adTypeName' }"
                            :show-all-levels="false"
                            @change="changeAdType"></el-cascader>
                    </el-form-item>
                    <el-form-item label="广告标题">
                        <el-input v-model="filter.title" placeholder="请输入条件查询" @change="getPage()"/>
                    </el-form-item>

                    <el-form-item>
                        <el-button type="primary" @click="getPage()" icon="el-icon-search">查询</el-button>
                        <el-button @click="showUpdate({})"icon="el-icon-plus">添加</el-button>

                    </el-form-item>
                </el-col>
                <el-col :span="4" style="text-align: right;">
                    <el-form-item style="margin-right:0px;">
                        <el-button @click="toAdType()" icon="el-icon-folder">分类管理</el-button>
                    </el-form-item>
                </el-col>
            </el-row>

        </el-form>
        <div class="form-content">
            <el-table
                v-loading="loading"
                style="width: 100%"
                :data="page.records"
                border
                stripe
                :cell-class-name="getCellIndex"
                @cell-click="tableDbEdit"
                :header-cell-style = "headerCellStyle"
            >
                <el-table-column prop="id" label="ID" width="60"/>
                <el-table-column width="80" prop="image" label="图片">
                    <template #default="scope">
                        <el-image style="width: 50px; height: 50px" :src="scope.row.image" fit="fit"
                                  :preview-src-list="srcList" :initial-index="scope.$index">
                            <div slot="error" class="image-slot"
                                 style="display: flex;justify-content: center;align-items: center;width: 100%;height: 100%;background: #f5f7fa;color: #909399;font-size: 20px;">
                                <i class="el-icon-picture-outline"></i>
                            </div>
                        </el-image>
                    </template>
                </el-table-column>
                <el-table-column prop="adTypeName" label="分类"/>
                <el-table-column prop="title" label="广告标题"/>
                <el-table-column prop="url" label="链接"/>
                <el-table-column width="80px" property="sort" sortable label="排序">
                    <template #default="scope">
                    <span v-show="!(scope.row.index == coordinate.row && scope.column.index ==coordinate.column)">
                      {{ scope.row.sort }}
                    </span>
                        <el-input
                            v-show="scope.row.index == coordinate.row && scope.column.index ==coordinate.column"
                            ref="sort"
                            v-model="scope.row.sort"
                            size="mini"
                            autofocus
                            @change="submitTableEdit(scope.row)"
                        />
                    </template>
                </el-table-column>
                <el-table-column width="70" label="状态">
                    <template #default="scope">
                        <el-switch
                            v-model="scope.row.status"
                            @change="statusSwitch(scope.row.status,scope.row.id)"
                        />
                    </template>
                </el-table-column>
                <el-table-column width="80" prop="updateName" label="更新人"/>
                <el-table-column width="160" prop="updateTime" label="更新时间"/>

                <el-table-column fixed="right" label="操作" width="160">
                    <template #default="scope">
                        <el-button size="mini" style="margin:0 0px 10px 10px;" @click="showUpdate(scope.row)">编辑
                        </el-button>

                        <el-popconfirm title="确定删除吗？" @onConfirm="handleDelete(scope.row.id)">
                            <el-button slot="reference" size="mini" style="margin:0 0px 10px 10px;">删除
                            </el-button>
                        </el-popconfirm>

                    </template>
                </el-table-column>
            </el-table>

            <el-pagination
                background
                :current-page="page.current"
                :page-sizes="[10, 20, 30, 50,100]"
                :page-size="page.size"
                layout="total, sizes, prev, pager, next, jumper"
                :total="page.total"
                style="margin-top:20px;"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
            />

            <Update ref="updateRef" @refresh="getPage()"/>
        </div>
    </section>
</template>
<script>
import {AdApi} from '@/api/cms/Ad'
import {AdTypeApi} from "@/api/cms/AdType";
import Update from './Update.vue'

export default {
    name: 'CmsAdList',
    components: {
        Update
    },
    data() {
        return {
            page: {
                total: 0,
                size: 10,
                current: 1,
                pages: 0,
                records: []
            },
            filter: {
                title: '',
                adTypeId: '',
                cascaderPath: []
            },
            loading: false,
            coordinate: {column: -1, row: -1},
            srcList: [],
            adTypeList:[],
            headerCellStyle:{
                "background-color":"#F2F5FC"
            }
        }
    },
    created() {
        AdTypeApi.getAllList().then((response) => {
            response.unshift({id: '', adTypeName: '全部'})
            this.adTypeList = response
        })
        this.getPage()
    },
    methods: {
        // 获取用户列表
        getPage() {
            const params = {
                pageIndex: this.page.current,
                pageSize: this.page.size,
                title: this.filter.title,
                adTypeId:this.filter.adTypeId
            }

            this.loading = true
            AdApi.getPage(params)
                .then((response) => {
                    this.page = response
                    this.loading = false

                    response.records.forEach(item => {
                        this.srcList.push(item.image)
                    })
                }).catch((err) => {
                this.loading = false
            })
        },

        // 删除数据
        handleDelete(id) {
            AdApi.delete(id).then(() => {
                    this.getPage()
                })
        },

        handleSizeChange(val) {
            this.page.size = val
            this.page.current = 1
            this.getPage()
        },

        handleCurrentChange(val) {
            this.page.current = val
            this.getPage()
        },

        showUpdate(data) {
            this.$refs['updateRef'].showUpdate(data)
        },
        // 修改状态
        statusSwitch(val, id) {
            const params = {
                id: id,
                status: val
            }

            AdApi.updateStatus(params).then((response) => {
            })
        },
        // 修改排序
        submitTableEdit(row) {
            this.coordinate.column = -1
            this.coordinate.row = -1
            const params = {
                id: row.id,
                sort: row.sort
            }
            AdApi.updateSort(params).then((response) => {
            })
        },
        getCellIndex({row, column, rowIndex, columnIndex}) {
            row.index = rowIndex
            column.index = columnIndex
        },
        tableDbEdit(row, column) {
            this.coordinate.row = row.index
            this.coordinate.column = column.index
        },
        changeAdType(e) {
            this.filter.adTypeId = e[e.length - 1]
        },
        toAdType() {
            this.$router.push('/cms/adType')
        },

    }
}

</script>

<style lang="scss" scoped></style>
